package com.uton.rfidcontroller.view.base;

import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.model.CarDealerListModel;
import com.uton.rfidcontroller.view.adapter.task.TaskListHasDoAdapter;
import com.uton.rfidcontroller.viewmodel.base.BaseViewModel;

import java.util.List;

import butterknife.BindView;

/**
 * Created by 王家宝 on 2017/9/5.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public abstract class BaseRecyclerViewFragment<VDB extends ViewDataBinding, VM extends BaseViewModel, TM>
        extends BaseFragment<VDB, VM> {

    @BindView(R.id.xrefreshview)
    XRefreshView mXRefreshView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    protected TaskListHasDoAdapter mAdapter;

    protected LinearLayoutManager layoutManager;


    @Override
    public void doSomeThing() {
        initRecyclerView();
    }

    protected abstract void doOnRefresh();

    protected abstract void doOnLoadMore();

    protected void initRecyclerView() {
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new <TM>TaskListHasDoAdapter(this);
        // 设置静默加载模式
//        xRefreshView1.setSilenceLoadMore();
        layoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(layoutManager);
        // 静默加载模式不能设置footerview
        mRecyclerView.setAdapter(mAdapter);
        //设置刷新完成以后，headerview固定的时间
        mXRefreshView.setPinnedTime(1000);
        mXRefreshView.setMoveForHorizontal(true);
        //禁止上拉加载更多
        mXRefreshView.setPullLoadEnable(false);
        mXRefreshView.setAutoLoadMore(false);
//        mAdapter.setCustomLoadMoreView(new XRefreshViewFooter(getContext()));
        mXRefreshView.enableReleaseToLoadMore(true);
        mXRefreshView.enableRecyclerViewPullUp(true);
        mXRefreshView.enablePullUpWhenLoadCompleted(true);
        mXRefreshView.setHideFooterWhenComplete(false);
        //设置静默加载时提前加载的item个数
//        xRefreshView1.setPreLoadCount(4);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh(boolean isPullDown) {
                doOnRefresh();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                doOnLoadMore();
            }
        });


    }

    public TaskListHasDoAdapter getAdapter() {
        return mAdapter;
    }

    public void stopLoadMore(boolean isComplete) {
        if (isComplete) {
            mXRefreshView.setLoadComplete(true);
        } else {
            mXRefreshView.stopLoadMore(true);
        }
    }

    public void stopRefresh() {
        mXRefreshView.stopRefresh();
    }

    public void refreshComplete(@NonNull List<CarDealerListModel> tempData) {
        mAdapter.clear();
        mAdapter.setData(tempData);
        mXRefreshView.stopRefresh();

        if (tempData.size() < GlobalConstants.PAGE_SIZE) {
            mXRefreshView.setLoadComplete(true);
        } else {
            mXRefreshView.setLoadComplete(false);
        }
    }


}
